/**
 * 
 */
package com.dhcc.android.base.common.bean;

import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.dhcc.android.base.R;
import com.dhcc.android.base.common.adapter.SortAdapter;
import com.dhcc.android.base.common.view.SideBar;
import com.dhcc.android.base.utils.CharacterParser;
import com.dhcc.android.base.utils.ClearEditText;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.PinyinComparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * 处理按首字母分类的数据
 * 
 * @author pengling
 * @date 2014-10-23
 */
public class SortDatas {
	private CharacterParser characterParser;
	private List<SortModel> SourceDateList;
	ListView sortListView;
	SortAdapter adapter;
	/**
	 * @author 根据拼音来排列ListView里面的数据类
	 */
	private PinyinComparator pinyinComparator;

	public SortAdapter initViews(List<SortModel> sortModelList,
			OnItemClickListener onItemClickListener, View view,
			Activity activity, Boolean isSingle, String beanType) {
		// 实例化汉字转拼音类
		characterParser = CharacterParser.getInstance();

		pinyinComparator = new PinyinComparator();

		SideBar sideBar = (SideBar) view.findViewById(R.id.sidrbar);
		TextView dialog = (TextView) view.findViewById(R.id.dialog);
		sideBar.setTextView(dialog);

		// 设置右侧触摸监听
		sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
			@Override
			public void onTouchingLetterChanged(String s) {
				// 该字母首次出现的位置
				int position = adapter.getPositionForSection(s.charAt(0));
				if (position != -1) {
					sortListView.setSelection(position);
				}
			}
		});

		sortListView = (ListView) view.findViewById(R.id.country_lvcountry);
		sortListView.setOnItemClickListener(onItemClickListener);

		SourceDateList = filledData(sortModelList, beanType);

		// 根据a-z进行排序源数据
		if(!beanType.equals(Constants.SortBeanTypeByField)){
			Collections.sort(SourceDateList, pinyinComparator);
		}
		if (adapter == null) {
			adapter = new SortAdapter(activity, SourceDateList, isSingle,
					beanType);
			sortListView.setAdapter(adapter);
		} else {
			adapter.updateListView(SourceDateList);
		}

		ClearEditText mClearEditText = (ClearEditText) view
				.findViewById(R.id.filter_edit);

		// 根据输入框输入值的改变来过滤搜索
		mClearEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// 当输入框里面的值为空，更新为原来的列表，否则为过滤数据列表
				filterData(s.toString());
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
		return adapter;
	}

	/**
	 * 为ListView填充数据
	 * 
	 * @author
	 * @param date
	 * @return
	 */
	private List<SortModel> filledData(List<SortModel> sortModelList,
			String beanType) {
		List<SortModel> mSortList = new ArrayList<SortModel>();
		for (SortModel sortModel : sortModelList) {
				if(!beanType.contains(Constants.SortBeanTypeByField)){
					// 汉字转换成拼音
					String pinyin = characterParser.getSelling(sortModel
							.getName());
					
					String sortString = pinyin.substring(0, 1).toUpperCase();
					
					// 正则表达式，判断首字母是否是英文字母
					if (sortString.matches("[A-Z]")) {
						sortModel.setSortLetters(sortString.toUpperCase());
					} else {
						sortModel.setSortLetters("#");
					}
				}
				mSortList.add(sortModel);
		}
		
		return mSortList;
	}

	/**
	 * 根据输入框中的值来过滤数据并更新ListView
	 * 
	 * @author
	 * @param filterStr
	 */
	private void filterData(String filterStr) {
		List<SortModel> filterDateList = new ArrayList<SortModel>();

		if (TextUtils.isEmpty(filterStr)) {
			filterDateList = SourceDateList;
		} else {
			filterDateList.clear();
			for (SortModel sortModel : SourceDateList) {
			    String key = sortModel.getId();
				String name = sortModel.getName();
				if (name.indexOf(filterStr.toString()) != -1
						|| characterParser.getSelling(name).startsWith(
								filterStr.toString()) || key.contains(filterStr)) {
					filterDateList.add(sortModel);
				}
			}
		}
		// 根据a-z进行排序
		Collections.sort(filterDateList, pinyinComparator);
		adapter.updateListView(filterDateList);
	}

	public List<SortModel> getSortModelListFromKeyValue(
			List<KeyValueBean> keyValueList) {
		List<SortModel> sortModelList = new ArrayList<SortModel>();
		for (KeyValueBean keyValueBean : keyValueList) {
			SortModel sortModel = new SortModel();
			sortModel.setId(keyValueBean.getKey());
			sortModel.setName(keyValueBean.getValue());
			if(keyValueBean.getFieldset()==null){
				sortModel.setSortLetters("");
			}else{
				sortModel.setSortLetters(keyValueBean.getFieldset());
			}
			sortModelList.add(sortModel);
		}
		return sortModelList;
	}

//	public List<SortModel> getSortModelListFromUser(List<UserBean> userList) {
//		List<SortModel> sortModelList = new ArrayList<SortModel>();
//		for (UserBean userBean : userList) {
//			SortModel sortModel = new SortModel();
//			sortModel.setId(userBean.getId());
//			sortModel.setName(userBean.getName());
//			sortModel.setDepartment(userBean.getDepartment());
//			sortModel.setPhoto(userBean.getPhoto());
//			sortModelList.add(sortModel);
//		}
//		return sortModelList;
//	}
}
